from extensions import db
from datetime import datetime

class SafetyInspection(db.Model):
    __tablename__ = 'safety_inspections'
    
    id = db.Column(db.Integer, primary_key=True)
    inspection_no = db.Column(db.String(50), unique=True, nullable=False)
    product_name = db.Column(db.String(100), nullable=False)
    batch_number = db.Column(db.String(50), nullable=False)
    inspection_type = db.Column(db.String(50), nullable=False)  # routine, special, followup
    inspector = db.Column(db.String(50), nullable=False)
    inspection_date = db.Column(db.DateTime, nullable=False)
    temperature = db.Column(db.Float)
    humidity = db.Column(db.Float)
    result = db.Column(db.String(20), nullable=False)  # passed, failed, pending
    issues_found = db.Column(db.Text)
    recommendations = db.Column(db.Text)
    next_inspection_date = db.Column(db.DateTime)
    created_at = db.Column(db.DateTime, default=datetime.now)
    created_by = db.Column(db.Integer, db.ForeignKey('users.id'))
    notes = db.Column(db.Text)

class SafetyIncident(db.Model):
    __tablename__ = 'safety_incidents'
    
    id = db.Column(db.Integer, primary_key=True)
    incident_no = db.Column(db.String(50), unique=True, nullable=False)
    incident_type = db.Column(db.String(50), nullable=False)  # contamination, temperature, damage
    severity = db.Column(db.String(20), nullable=False)  # low, medium, high, critical
    product_name = db.Column(db.String(100), nullable=False)
    batch_number = db.Column(db.String(50), nullable=False)
    occurrence_date = db.Column(db.DateTime, nullable=False)
    discovery_date = db.Column(db.DateTime, nullable=False)
    location = db.Column(db.String(200), nullable=False)
    description = db.Column(db.Text, nullable=False)
    immediate_action = db.Column(db.Text)
    root_cause = db.Column(db.Text)
    corrective_action = db.Column(db.Text)
    status = db.Column(db.String(20), default='open')  # open, investigating, resolved, closed
    resolved_date = db.Column(db.DateTime)
    created_at = db.Column(db.DateTime, default=datetime.now)
    created_by = db.Column(db.Integer, db.ForeignKey('users.id'))
    handler_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    notes = db.Column(db.Text) 